package com.shrct.businessprocess.project;

import com.shrct.businessprocess.common.RCGeneralCreditData;
import com.shrct.businessprocess.organization.RCEnterprise;
import com.shrct.businessprocess.project.IHasCreditStat;
import com.shrct.businessprocess.project.RCProjectCreditStat;
import com.shrct.businessprocess.project.RCProjectCreditStatService;
import com.shrct.businessprocess.project.RCTotalProjectCreditStat;
import com.shrct.businessprocess.project.proxy.RCCustomerCreditAdaptorProxy;
import edu.fudan.mylang.pf.EntityAdaptor;
import edu.fudan.mylang.pf.IGenericQuery;
import java.util.Collection;
import java.util.List;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.BeanFactory;

public class RCCustomerCreditAdaptor extends EntityAdaptor<RCEnterprise> implements IHasCreditStat {
  public RCCustomerCreditAdaptor() {
    super();
  }
  
  public RCCustomerCreditAdaptor(final RCEnterprise adaptee) {
    this.adaptee = adaptee;
  }
  
  public RCCustomerCreditAdaptorProxy toProxy() {
    return new RCCustomerCreditAdaptorProxy(adaptee.toProxy());
  }
  
  public RCTotalProjectCreditStat getTotalProjectCreditStat() {
    RCTotalProjectCreditStat _xblockexpression = null;
    {
      IGenericQuery<RCProjectCreditStat> query=getObjectFactory().createGenericQuery(RCProjectCreditStat.class,"select e from com.shrct.businessprocess.project.impl.RCProjectCreditStatImpl as e   where ((e.project.customer) = (:adaptee)) and  e.active = 1      ").setParameter("adaptee",adaptee);
      final Collection<RCProjectCreditStat> stats = query.list();
      BeanFactory _beanFactory = this.getBeanFactory();
      RCProjectCreditStatService _bean = _beanFactory.<RCProjectCreditStatService>getBean(RCProjectCreditStatService.class);
      final Function1<RCProjectCreditStat,RCGeneralCreditData> _function = new Function1<RCProjectCreditStat,RCGeneralCreditData>() {
          public RCGeneralCreditData apply(final RCProjectCreditStat it) {
            RCGeneralCreditData _rCGeneralCreditData = new RCGeneralCreditData(it, Double.valueOf(100d));
            return _rCGeneralCreditData;
          }
        };
      Iterable<RCGeneralCreditData> _map = IterableExtensions.<RCProjectCreditStat, RCGeneralCreditData>map(stats, _function);
      List<RCGeneralCreditData> _list = IterableExtensions.<RCGeneralCreditData>toList(_map);
      RCTotalProjectCreditStat _generateTotalStat = _bean.generateTotalStat(_list);
      final Procedure1<RCTotalProjectCreditStat> _function_1 = new Procedure1<RCTotalProjectCreditStat>() {
          public void apply(final RCTotalProjectCreditStat it) {
            String _name = RCCustomerCreditAdaptor.this.adaptee.getName();
            it.setTitle(_name);
          }
        };
      RCTotalProjectCreditStat _doubleArrow = ObjectExtensions.<RCTotalProjectCreditStat>operator_doubleArrow(_generateTotalStat, _function_1);
      _xblockexpression = (_doubleArrow);
    }
    return _xblockexpression;
  }
}
